package ro.uaic.info.fmse.context.transformers.context;
import java.util.ArrayList;
import java.util.List;
public class Mapping {
	private List<Pair> pairs;
	public void add(Pair pair) {
		if (pairs == null)
			pairs = new ArrayList<Pair>();
		pairs.add(pair);
	}
	public void add_front(Pair pair) {
		if (pairs == null)
			pairs = new ArrayList<Pair>();
		pairs.add(0, pair);
	}
	
	public List<Pair> get() {
		return pairs;
	}
	
	@Override
	public String toString() {
		String out = "";
		for (Pair pair : pairs)
			out += "(" + pair.cnode + ", " + pair.rnode + ");";
		return "{" + out + "}";
	}
	public static Mapping collapse(Mapping m_mset, Mapping s_sons) {
		try {
			
			// put two maps into a single map
			Mapping m = new Mapping();
			for (Pair p : m_mset.pairs)
				m.add((Pair) p.clone());
			for (Pair p : s_sons.pairs)
				m.add((Pair) p.clone());
			return m;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<Mapping> distribute(List<Mapping> mset) {
		List<Mapping> result = new ArrayList<Mapping>();
		
		for(Mapping m:mset)
			result.add(collapse(this, m));
		
		return result;
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Mapping m = new Mapping();
		for(Pair pair:pairs)
			m.add((Pair) pair.clone());
		return m;
	}
	public int getConfigNode(int ruleID) {
		for (Pair p:pairs)
			if (p.rnode == ruleID)
				return p.cnode;
		return -1;
	}
}